package com.xsherl.leetcode.solution;

public class RomanToInt {

    public static int[] radix = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
    public static String[] roman = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};

    public int romanToInt(String s) {
        int sum = 0;
        int index = 0;
        while (index < s.length()) {
            for (int i = roman.length - 1; i >= 0; --i) {
                String r = roman[i];
                if (s.startsWith(r, index)) {
                    index += r.length();
                    sum += radix[i];
                    break;
                }
            }
        }
        return sum;
    }
}
